const express = require('express')
const app = express()
const port = 3000
const bodyParser = require("body-parser")
var dbserver = require('./dao/dbserver.js')
var emailserver = require('./dao/emailserver.js')
var jwt = require('./dao/jwt.js')

//使用bodyParser插件
app.use(bodyParser.json());
//添加静态地址
app.use(express.static(__dirname + '/data'))
//设置允许跨域访问该服务.
app.all('*', function(req, res, next) {
	res.header('Access-Control-Allow-Origin', '*');
	//Access-Control-Allow-Headers ,可根据浏览器的F12查看,把对应的粘贴在这里就行
	res.header('Access-Control-Allow-Headers', 'Content-Type');
	res.header('Access-Control-Allow-Methods', '*');
	res.header('Content-Type', 'application/json;charset=utf-8');
	next();
});

// token验证
app.use(function(req, res, next) {
	if (req.body.token != undefined) {
		let token = req.body.token;
		let tokenMatch = jwt.verifyToken(token);
		if (tokenMatch == 0) {
			next()
		} else {
			res.send({token_status:300})
		}
	} else {
		next()
	}
});

// 调用路由文件
require('./router/router.js')(app);
require('./router/files.js')(app);

app.listen(port, () => console.log(`服务器启动成功，端口号为： ${port}!`))
